package icu.bughub.app.app.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import icu.bughub.app.app.ui.screens.MainFrame
import icu.bughub.app.app.ui.theme.Blue200
import icu.bughub.app.app.ui.theme.Blue700
import icu.bughub.app.app.ui.theme.JetpackComposeBasicTheme

/**
 * @author huangzehui
 * @date 2024/5/14 13:30 周二
 * @description
 */

/**
 * 统一标题栏
 *
 * @param content 标题栏内容
 */
@Composable
fun TopAppBar(
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit
) {
    val systemUiController = rememberSystemUiController()

    LaunchedEffect(key1 = Unit) {
        systemUiController.setStatusBarColor(color = Color.Transparent)
    }

    // 标题栏高度
    val appBarHeight = 46.dp

    // 转换状态栏高度为 dp
    val statusBarHeightDp = with(LocalDensity.current) {
//        statusBarHeight.toDp()
        // 获取状态栏的高度
        WindowInsets.statusBars.getTop(this).toDp()
    }


    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(
                brush = Brush.linearGradient(
                    colors = listOf(
                        Blue700,
                        Blue200
                    )
                )
            )
            .height(appBarHeight + statusBarHeightDp)
            .padding(top = statusBarHeightDp)
            .then(modifier), // 连接另外的一个 Modifier, 这样外面设置了 padding，也不会让背景也有padding的作用，顺序很重要，一定在背景之后调用
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        content()
    }
}

@Preview
@Composable
fun TopAppBarPreview() {
    JetpackComposeBasicTheme {
        TopAppBar() {
            Text(text = "Hello")
        }
    }

}